package com.zshan.clinic.web.controller.medical;


import com.zshan.clinic.admin.entity.dto.AddInfusionFeeDTO;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.service.medical.OrderExecService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.OrderExecRecord;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 执行单
 * @author lichen
 */
@RestController
@RequestMapping("/order/exec")
public class OrderExecController extends AbstractController {


    @Autowired
    private OrderExecService orderExecService;

    /**
     * 执行：查询收费记录
     * @param orderId 执行单ID
     * @return
     */
    @GetMapping("/getChargeList")
    public RestResp<List<ExecChargeItemVo>> getChargeList(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        List<ExecChargeItemVo> execList = orderExecService.getChargeList(userInfo.getClinicId(), orderId);
        return RestResp.SUCCESS(execList);
    }

    /**
     * 患者：查询患者执行记录
     * @param patientId
     * @param visitDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPatientExecList")
    public RestResp<PageInfo<OrderExecPatientVo>> getPatientExecList(@RequestParam(name = "patientId") Long patientId,
                                                                     @RequestParam(name = "visitDate",required = false) LocalDate visitDate,
                                                                     @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                     @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<OrderExecPatientVo> execList = orderExecService.getPatientExecList(userInfo.getClinicId(), patientId,visitDate,pageNo,pageSize);
        return RestResp.SUCCESS(execList);
    }

    /**
     * 执行：查询诊所今日待执行数
     * @return
     */
    @GetMapping("/getWaitingCount")
    public RestResp<Long> getWaitingCount() {
        UserInfo userInfo = getUserInfo();
        return RestResp.SUCCESS(orderExecService.getWaitingCount(userInfo.getClinicId()));
    }


    /**
     * 执行：获取执行单列表
     * @param patientName
     * @param visitDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getExecList")
    public RestResp<List<OrderExecPageVo>> getExecList(@RequestParam(name = "execCategory", required = false) String execCategory,
                                                       @RequestParam(name = "patientName", required = false) String patientName,
                                                       @RequestParam(name = "visitDate", required = false) LocalDate visitDate,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderExecPageVo> list = orderExecService.getExecList(userInfo.getClinicId(),execCategory, patientName, visitDate, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }


    /**
     * 执行：查询执行单详情
     * @param orderId 订单ID，从列表中获取
     * @return
     */
    @GetMapping("/getExecDetail")
    public RestResp<OrderExecDetailVo> getExecDetail(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        OrderExecDetailVo execDetail = orderExecService.getExecDetail(userInfo.getClinicId(),orderId);
        return RestResp.SUCCESS(execDetail);
    }


    /**
     * 执行：获取执行历史记录
     * @param orderId 执行单ID
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getRecordList")
    public RestResp<List<OrderExecRecordVo>> getRecordList(@RequestParam(name = "orderId") Long orderId,
                                                           @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderExecRecordVo> list = orderExecService.getRecordList(userInfo.getClinicId(), orderId, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }


    /**
     * 执行：添加执行记录
     * @param orderExecRecord
     * @return
     */
    @PostMapping("/addExecRecord")
    public RestResp<Void> addExecRecord(@RequestBody OrderExecRecord orderExecRecord) {
        UserInfo userInfo = getUserInfo();
        orderExecRecord.setCreateUserId(userInfo.getEmpId());
        orderExecRecord.setCreateBy(userInfo.getEmpName());
        orderExecRecord.setClinicId(userInfo.getClinicId());
        orderExecService.addExecRecord(orderExecRecord);
        return RestResp.SUCCESS();
    }

    /**
     * 执行：修改执行记录
     * @param orderExecRecord
     * @return
     */
    @PostMapping("/updateExecRecord")
    public RestResp<Void> updateExecRecord(@RequestBody OrderExecRecord orderExecRecord) {
        UserInfo userInfo = getUserInfo();
        orderExecRecord.setUpdateUserId(userInfo.getEmpId());
        orderExecRecord.setUpdateBy(userInfo.getEmpName());
        orderExecRecord.setClinicId(userInfo.getClinicId());
        orderExecService.updateExecRecord(orderExecRecord);
        return RestResp.SUCCESS();
    }
    /**
     * 执行：添加注射费
     * @param addInfusionFeeDTO
     * @return
     */
    @PostMapping("/addInfusionFee")
    public RestResp<Void> addInfusionFee(@RequestBody AddInfusionFeeDTO addInfusionFeeDTO) {
        UserInfo userInfo = getUserInfo();
        addInfusionFeeDTO.setUserId(userInfo.getEmpId());
        addInfusionFeeDTO.setUserBy(userInfo.getEmpName());
        addInfusionFeeDTO.setClinicId(userInfo.getClinicId());
        orderExecService.addInfusionFee(addInfusionFeeDTO);
        return RestResp.SUCCESS();
    }

}
